<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      td {
        position: relative;
      }
      td .ok-btn {
        position: absolute;
        bottom: -20px;
        right: 50px;
        z-index: 999;
      }
      td .cancel-btn {
        position: absolute;
        bottom: -20px;
        right: 0;
        z-index: 999;
      }
    </style>
  </head>
  <body>
    <table border="1">
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
          <th>邮箱</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-edit>jack</td>
          <td data-edit>20</td>
          <td data-edit>jack@qq.com</td>
        </tr>
        <tr>
          <td>lili</td>
          <td>20</td>
          <td>lili@qq.com</td>
        </tr>
      </tbody>
    </table>
    <script>
      document.onclick = function (e) {
        const target = e.target;
        const edit = target.dataset.edit;
        if (edit !== undefined) {
          // 保存当前单元格的内容
          const tdContent = target.innerText;
          // 将单元格置空
          target.innerHTML = "";
          // 创建输入框
          const input = document.createElement("input");
          // 阻止冒泡
          input.onclick = (e) => e.stopPropagation();
          // 将当前内容放入input中
          input.value = tdContent;
          // 使用input填充单元格
          target.append(input);
          // 获取焦点
          input.focus();
          // 创建确定
          const okBtn = document.createElement("button"); //<button type="button">确定</button>
          okBtn.classList.add("ok-btn");

          okBtn.type = "button";
          okBtn.innerHTML = "确定";
          // 将确定按钮置入单元格
          target.append(okBtn);

          // 创建取消
          const cancelBtn = document.createElement("button"); //<button type="button">取消</button>
          cancelBtn.classList.add("cancel-btn");
          cancelBtn.type = "button";
          cancelBtn.innerHTML = "取消";
          // 将取消按钮置入单元格
          target.append(cancelBtn);
        }
      };
    </script>
  </body>
</html>
